#!/bin/sh
get_current_version() {
    CUR_VER=$(grep '^<addon' "$1"|sed -e 's/^\(.*[[:space:]]*version="\)\([0-9.]*\)\(".*\)$/\2/g')
    echo $CUR_VER
}

set_new_version() {
    CUR_VER=$(get_current_version "$1")
    echo "File: $1, oldVer: $CUR_VER"
    if test $# -ge 2; then
        NEW_VER="$2"
    else
        VER_TAIL=${CUR_VER##*.}
        VER_TAIL=$(( VER_TAIL + 1 ))
        NEW_VER="${CUR_VER%.*}.$VER_TAIL"
    fi
    echo "Setting newVer: $NEW_VER"
    [[ $(uname -s) == 'Darwin' ]] && ioption=(-i "") || ioption=(-i)
    sed "${ioption[@]}" -e '/^<addon/ s/^\(.*[[:space:]]*version="\)\([0-9.]*\)\(".*\)$/\1'$NEW_VER'\3/g' "$1"
}

ADDON=`pwd`
ADDON="${ADDON##*/}"
XML_FILE="$ADDON/addon.xml"

DIR="${0%/*}"
cd "$DIR"

if test ! -f "$XML_FILE"; then
    echo "please run ../update.sh under the updated addon's root dir"
    exit -1
fi

set_new_version "$XML_FILE" "$@"

python ./addons_xml_generator.py  

